//
//  STSettingViewController.swift
//  Story
//
//  Created by 汤泽川 on 2019/5/10.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import SnapKit
import PKHUD

class STSettingViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        self.hidesBottomBarWhenPushed = true
        buildView()
    }
    
    func buildView() {
        let informationItem = STSettingItem(title: "个人信息") { [weak self] in
            let profileVC = STUserInfoViewController()
            profileVC.title = "我的信息"
            self?.navigationController?.pushViewController(profileVC, animated: true)
        }
        let resetPassword = STSettingItem(title: "修改密码") { [weak self] in
            let loginVC = STLoginViewController()
            loginVC.stateObverser.accept(.change)
            loginVC.username.text = STUserService.shareInstance.currentUser.value?.username
            loginVC.username.isEnabled = false
            loginVC.email.text = STUserService.shareInstance.currentUser.value?.email
            loginVC.email.isEnabled = false
            self?.present(loginVC, animated: true, completion: nil)
        }
        let about = STSettingItem(title: "关于我们") {
        }
        let delegatePage = STSettingItem(title: "用户协议") {
        }

        var lastItem : UIView?
        for item in [informationItem, resetPassword, about, delegatePage] {
            self.view.addSubview(item)
            item.snp.makeConstraints { (make) in
                make.left.right.equalToSuperview()
                make.height.equalTo(60)
                if lastItem == nil {
                    make.top.equalToSuperview().offset(statusBarHeight + 44)
                } else {
                    make.top.equalTo(lastItem!.snp_bottom)
                }
            }
            lastItem = item
        }
        
        let logoutBtn = UIButton()
        logoutBtn.setTitle("退出登陆", for: .normal)
        logoutBtn.backgroundColor = .gray
        logoutBtn.titleLabel?.font = .boldSystemFont(ofSize: 21)
        logoutBtn.addTarget(self, action: #selector(didClickLogoutBtn), for: .touchUpInside)
        self.view.addSubview(logoutBtn)
        logoutBtn.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(60 + self.view.safeAreaInsets.bottom)
        }
        
        let copyRightLabel = UILabel()
        copyRightLabel.text = "当前版本 0.0.1"
        copyRightLabel.textColor = .gray
        copyRightLabel.font = .systemFont(ofSize: 9)
        self.view.addSubview(copyRightLabel)
        copyRightLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(logoutBtn.snp_top).offset(-5)
            make.centerX.equalToSuperview()
        }
        
    }
    
    @objc func didClickLogoutBtn() {
        HUD.show(.progress)
        STUserService.shareInstance.logout().result { (error) in
            HUD.hide()
            if let error = error {
                HUD.flash(.labeledError(title: nil, subtitle: error.localizedDescription))
            } else {
                HUD.flash(.success)
                self.navigationController?.popViewController(animated: true)
            }
        }
    }
}

class STSettingItem: UIView {
    let disposeBag = DisposeBag()
    
    init(title : String, action : (() -> Void)?) {
        super.init(frame: CGRect.zero)
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.font = .systemFont(ofSize: 14)
        titleLabel.textColor = .gray
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(18)
        }
        
        let arrowImageView = UIImageView()
        arrowImageView.image = #imageLiteral(resourceName: "right_icon")
        self.addSubview(arrowImageView)
        arrowImageView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-18)
            make.size.equalTo(CGSize(width: 30, height: 30))
        }
        
        let line = UIView()
        line.backgroundColor = UIColor(white: 0, alpha: 0.1)
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        let alphaBtn = UIButton()
        self.addSubview(alphaBtn)
        alphaBtn.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        alphaBtn.rx.controlEvent(.touchUpInside).subscribe(onNext: {
            if action != nil {
                action!()
            }
        }).disposed(by: disposeBag)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
